export default {
	// 为当前模块开启命名空间
	namespaced: true,

	// 模块的 state 数据
	state: {
		songDetails: {
			imageSrc: '',
			audioSrc: '',
			audioName: '',
			singerName: '',
		},
		isbtom: false,
		isplay: false,
		show: false, //是否展示
		isaudio: false, //歌单详情显示
		startY: 0, // 触摸开始时的Y坐标
		moveY: 0, // 触摸点移动的距离
		list_moveBy: 0, //歌单列表y坐标
		lyric_moveby: 0, //歌曲详情页y坐标
		threshold: 110, // 下拉关闭的阈值
		dragging: false, // 是否正在拖动
		// indexList: [],
		urls: [{
				src: 'https://cdn.uviewui.com/uview/album/1.jpg',
				mp3: 'http://freetyst.nf.migu.cn/public/product10th/productB36/2024/03/1213/2023%E5%B9%B403%E6%9C%8801%E6%97%A515%E7%82%B915%E5%88%8640%E7%A7%92818%E6%AF%AB%E7%A7%92%E6%B5%B7%E8%9D%B6%E5%94%B1%E7%89%87%E8%BF%81%E7%A7%BB%E5%87%86%E5%85%A5%E9%9F%B3%E4%B9%901%E9%A6%96/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/6005861HWQW130050.mp3?channelid=02&msisdn=b9f2290d-989f-4b27-9670-32d43bcaec3e&Tim=1716249981674&Key=580d78bdb14fa28c',
				name: '江南',
				singerName: '林俊杰',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/37/vn/ab3574345b444d4690ba9cedf3e7b355'
			},
			{
				src: 'https://cdn.uviewui.com/uview/album/2.jpg',
				mp3: 'http://freetyst.nf.migu.cn/public/product10th/productB36/2021/11/0316/2009%E5%B9%B412%E6%9C%8808%E6%97%A5%E6%B5%B7%E8%9D%B6%E5%94%B1%E7%89%87/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/60058622727160700.mp3?channelid=02&msisdn=93c00bd1-df65-4ed2-be9d-59593d54f154&Tim=1716253683719&Key=5b7d35c4e5ca4255',
				name: '背对背拥抱',
				singerName: '林俊杰',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/2n/tt/1j'
			},
			{
				src: 'https://cdn.uviewui.com/uview/album/3.jpg',
				mp3: 'http://freetyst.nf.migu.cn/public/product9th/product46/2024/03/1816/2023%E5%B9%B407%E6%9C%8812%E6%97%A511%E7%82%B910%E5%88%8632%E7%A7%92221%E6%AF%AB%E7%A7%92%E4%BA%9A%E7%A5%9ECP%E8%BF%81%E7%A7%BB%E5%87%86%E5%85%A5%E9%9F%B3%E4%B9%901%E9%A6%96/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/60057771645162514.mp3?channelid=02&msisdn=20c33956-bc74-4a70-a449-f0d15062fa78&Tim=1716250581621&Key=7db474f73c16b3b1',
				name: 'Letting Go',
				singerName: '陈老兮',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/39/6w/8196160f7aae4c95b1c7883850dc336e'
			},
			{
				src: 'https://d.musicapp.migu.cn/data/oss/resource/00/2k/kf/9x.webp',
				mp3: 'http://freetyst.nf.migu.cn/public/product9th/product45/2022/05/0916/2018%E5%B9%B411%E6%9C%8810%E6%97%A514%E7%82%B930%E5%88%86%E6%89%B9%E9%87%8F%E9%A1%B9%E7%9B%AE%E5%8D%8E%E7%BA%B397%E9%A6%96-13/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/6005751LC6X161159.mp3?channelid=02&msisdn=5e632c3d-ee37-44de-914f-b0e20a6a4044&Tim=1716249438552&Key=4ca4a5c742a499a0',
				name: '新地球',
				singerName: '林俊杰',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/1y/hd/oh'
			},
			{
				src: 'https://d.musicapp.migu.cn/data/oss/resource/00/2h/v6/pf.webp',
				mp3: 'http://freetyst.nf.migu.cn/public/product9th/product46/2023/07/2313/2022%E5%B9%B408%E6%9C%8816%E6%97%A500%E7%82%B937%E5%88%8621%E7%A7%92593%E6%AF%AB%E7%A7%92%E9%82%93%E7%B4%AB%E6%A3%8B%E5%B7%A5%E4%BD%9C%E5%AE%A442%E8%BF%81%E7%A7%BB%E5%87%86%E5%85%A5%E9%9F%B3%E4%B9%901%E9%A6%96/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/69031400023134756.mp3?channelid=02&msisdn=5349c675-3da7-4ee6-aa65-706c2dd55a25&Tim=1716253214417&Key=fdcae469cc5a59ec',
				name: '摩天动物园',
				singerName: '林俊杰,G.E.M.邓紫棋',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/2d/xo/9e'
			},
			{
				src: 'https://d.musicapp.migu.cn/data/oss/resource/00/2s/1h/46780cb8fee14362ba0381f5c235cf34.webp',
				mp3: 'http://freetyst.nf.migu.cn/public/product9th/product45/2021/11/0315/2021%E5%B9%B404%E6%9C%8808%E6%97%A511%E7%82%B948%E5%88%86%E7%B4%A7%E6%80%A5%E5%86%85%E5%AE%B9%E5%87%86%E5%85%A5%E5%8D%8E%E7%BA%B355%E9%A6%96/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/6005759Z0S5153807.mp3?channelid=02&msisdn=c963f5c6-1cfb-4c9c-adde-f0d1f02d6282&Tim=1716250193712&Key=acf918ccdabc9758',
				name: '交换余生',
				singerName: '林俊杰',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/1z/pi/7h'
			},
			{
				src: 'https://d.musicapp.migu.cn/data/oss/resource/00/32/1u/609e1fad977d4c00ae1df5e1a8e8c7c5.webp',
				mp3: 'http://freetyst.nf.migu.cn/public/product9th/product45/2022/05/2616/2019%E5%B9%B411%E6%9C%8829%E6%97%A501%E7%82%B935%E5%88%86%E6%89%B9%E9%87%8F%E9%A1%B9%E7%9B%AE%E6%AD%A3%E4%B8%9C74%E9%A6%96-1/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/6007500AF11165835.mp3?channelid=02&msisdn=58b6650a-6e7a-459f-8238-311eefde9bea&Tim=1716248355797&Key=ef53fe5012f5c23c',
				name: '泡沫',
				singerName: 'G.E.M.邓紫棋',
				lyric: 'http://d.musicapp.migu.cn/prod/file-service/file-down/4eedd78464c21ce789dea6928415b323/150e1e03d45c74edf27ee13ad7db6028/df26eaa17bf9bac2b97b1468263f6499'
			},
			{
				src: 'https://d.musicapp.migu.cn/data/oss/resource/00/2h/v6/pf.webp',
				mp3: 'http://freetyst.nf.migu.cn/public/product9th/product46/2023/07/2313/2022%E5%B9%B408%E6%9C%8815%E6%97%A523%E7%82%B903%E5%88%8619%E7%A7%92551%E6%AF%AB%E7%A7%92%E9%82%93%E7%B4%AB%E6%A3%8B%E5%B7%A5%E4%BD%9C%E5%AE%A442%E8%BF%81%E7%A7%BB%E5%87%86%E5%85%A5%E9%9F%B3%E4%B9%901%E9%A6%96/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/69031400007134749.mp3?channelid=02&msisdn=00d0c10c-c179-462e-9671-44b1c89dfa17&Tim=1716248241491&Key=f8bc2b7ba77d5c8c',
				name: '句号',
				singerName: 'G.E.M.邓紫棋',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/2d/y0/dk'
			},
			{
				src: 'https://d.musicapp.migu.cn/data/oss/resource/00/2s/da/581097ee985f49739158369a3d88ef70.webp',
				mp3: 'http://freetyst.nf.migu.cn/public/product9th/product45/2022/08/0211/2021%E5%B9%B411%E6%9C%8826%E6%97%A515%E7%82%B901%E5%88%86%E7%B4%A7%E6%80%A5%E5%86%85%E5%AE%B9%E5%87%86%E5%85%A5%E5%98%89%E7%BE%8E%E4%B9%B0%E6%96%AD4236%E9%A6%96190374/%E6%A0%87%E6%B8%85%E9%AB%98%E6%B8%85/MP3_128_16_Stero/69002603029112430.mp3?channelid=02&msisdn=11ba4097-8b9a-450a-b4f6-6e1abbab9df9&Tim=1716248782750&Key=6dff7d84519831dd',
				name: '倒数',
				singerName: 'G.E.M.邓紫棋',
				lyric: 'https://d.musicapp.migu.cn/data/oss/resource/00/2l/2s/jt'
			},
		],
		audio: uni.createInnerAudioContext(), //音乐组件
		Lrc: '', //原歌词
		ms: [], //整理后的歌词
		msIndex: 0, //当前播放的索引
		scrollIndex: 0, //当前选中歌词的索引
		srcollShow: false, //时间线显示
		likeSrc: '../../static/audio-icons/Like.png',
		islike: false, //是否喜欢
		playTime: 0, //歌曲当前进度
		currentTime: 0, //当前播放进度（用于监听）
		nowPlayTime: 0, //当前选中歌词的秒数
		animation_play_state: 'paused',
		animation_probe: '0deg',
		isanimation: false,
		playing: 0, //正在播放的歌曲
		cdFade: 1, //cd淡出
		lrcFade: 1, //歌词淡出
		isCore: true, //切换光盘页面
		scrollTop: 25, //垂直滚动条的位置
		PlayTimeing: false, //正在拖动进度条
		blockSize: 6, //进度条滑块的大小
		playMode: {
			imgSrc: '../../static/audio-icons/List_loop.png',
			name: '列表循环'
		},
		SelecMode: [{
				imgSrc: '../../static/audio-icons/List_loop.png',
				name: '列表循环'
			},
			{
				imgSrc: '../../static/audio-icons/Random_Play.png',
				name: '随机播放'
			},
			{
				imgSrc: '../../static/audio-icons/Single_loop.png',
				name: '单曲循环'
			}
		],
		history: JSON.parse(uni.getStorageSync('history') || '[]'),
		// cart: JSON.parse(uni.getStorageSync('cart') || '[]')
	},

	// 模块的 mutations 方法
	mutations: {
		updatePlayMode(state, data) {
			let currentIndex = state.SelecMode.findIndex(item => item.imgSrc === state.playMode.imgSrc);

			if (currentIndex === state.SelecMode.length - 1) {
				// 如果当前是数组的最后一个元素，则赋值为第一个元素
				state.playMode = state.SelecMode[0];
			} else {
				// 如果不是最后一个元素，则赋值为数组的下一个元素
				state.playMode = state.SelecMode[currentIndex + 1];
			}
		},
		updateHistory(state, data) {
			const exists = state.history.find(element => element === data);
			if (!exists) {
				state.history.push(data);
			}
			uni.setStorageSync('history', JSON.stringify(state.history))
		},
		deleteAllHistory(state) {
			state.history = []
			uni.setStorageSync('history', JSON.stringify(state.history))
		},
		play(state) {
			if (state.isanimation) return

			state.isanimation = true;
			if (state.isplay) {
				state.audio.pause()
			} else {
				state.audio.play()
			}
			state.isplay = !state.isplay
		},
		audioset(state) {
			state.audio.onPlay(() => {
				state.animation_play_state = 'running'
				state.animation_probe = '25deg'
				setTimeout(() => {
					state.isanimation = false;
					state.isplay = true
				}, 500);
			});
			state.audio.onPause(() => {
				setTimeout(() => {
					state.animation_play_state = 'paused'
					state.animation_probe = '0deg'
					state.isanimation = false;
				}, 500);
			})
			state.audio.onEnded(() => {
				this.commit('audio/ModePlayAudio')
				setTimeout(() => {
					state.animation_play_state = 'paused'
					state.animation_probe = '0deg'
				}, 500);
				state.isplay = false;
			})
			state.audio.onStop(() => {
				setTimeout(() => {
					state.animation_play_state = 'paused'
					state.animation_probe = '0deg'
				}, 500);
				state.isplay = false;
			})
			state.audio.onTimeUpdate(() => {
				state.currentTime = state.audio.currentTime
				if (state.PlayTimeing) return;
				state.playTime = state.audio.currentTime
			})
		},
		ModePlayAudio(state) {
			if (state.playMode.name === '列表循环') {
				if (state.urls[state.playing + 1]) {
					this.commit('audio/refreshAudio', {
						item: state.urls[state.playing + 1],
						index: state.playing + 1
					})
				}
			} else if (state.playMode.name === '随机播放') {
				let arr = [...state.urls];
				if(arr.length == 0){
					arr = [...state.urls];
				}
				
				const randomIndex = Math.floor(Math.random() * arr.length);
				const selectedElement = arr[randomIndex];
				if(selectedElement.mp3 === state.urls[state.playing].mp3){
					this.commit('audio/ModePlayAudio');
					return
				}
				
				arr.splice(randomIndex, 1); 
				
				let ind = 0;
				for (let i = 0; i < state.urls.length; i++) {
					if (state.urls[i].mp3 === selectedElement.mp3) {
						ind = i;
						break;
					}
				}
				this.commit('audio/refreshAudio', {
					item: selectedElement,
					index: ind
				})
			} else {
				state.playTime = 0 //清空显示进度
				state.scrollTop = 0; //歌词回滚
				state.msIndex = 0
				state.audio.play(); 
				state.audio.seek(state.playTime); //歌曲进度清空
			}
		},
		songDetailsAudioSrc(state, data) {
			state.audio.src = state.songDetails.audioSrc; //切换歌曲
			state.playTime = 0 //清空显示进度

			state.scrollTop = 0; //歌词回滚
			state.msIndex = 0
			if(data.oldVal != ''){				
				state.audio.play(); //切换自动播放
			}
			state.audio.seek(state.playTime); //歌曲进度清空

			// if (newVal) {
			// 	state.isbtom = true;
			// } else if (newVal == '') {
			// 	state.isbtom = false
			// }
		},
		refreshAudio(state, data) {
			state.songDetails.audioSrc = data.item.mp3;
			state.songDetails.imageSrc = data.item.src;
			state.songDetails.audioName = data.item.name;
			state.songDetails.singerName = data.item.singerName;

			this.commit('audio/requestLyric', data.item)
			state.playing = data.index;
		},
		async requestLyric(state, item) {
			const lrc = await new Promise((resolve, reject) => {
				uni.request({
					// url: 'http://10.160.151.6:8080/test/lyric?url='+item.lyric,
					url: 'http://xuanhao.free.idcfengye.com/api/lyric?url='+item.lyric,
					success: (res) => {
						resolve(res.data)
					},
				})
			})
			state.Lrc = lrc
			this.commit('audio/queryLrc');
		},
		addAudio(state, data) {
			state.urls.splice(state.playing + 1, 0, data);
		},
		deleteAudio(state, index) {
			state.urls.splice(index, 1)
		},
		updatePlayTimeing(state, val) {
			state.PlayTimeing = true;
			state.blockSize = 12
		},
		updatePlayTime(state, val) {
			state.blockSize = 6
			state.PlayTimeing = false;
			// state.audio.startTime = val
			state.audio.seek(val)
		},
		list(state) {
			state.show = !state.show
		},
		updateisAudio(state) {
			state.isaudio = !state.isaudio
		},
		updateUrls(state, data) {
			state.urls = data
			state.playing = 0;
			state.audio.pause();
			state.isplay = false;
		},
		modelPlayTime(state, newVal) {
			state.playTime = newVal;
		},
		loadmore(state) {
			if (state.songDetails.audioSrc) {
				state.isbtom = true;
			}
		},
		queryLrc(state) {
			state.ms = []
			if (state.Lrc.length == 0) return;
			// var lyricstxt = []; //存放歌词
			var lrcs = state.Lrc.split('\n'); //用回车拆分成数组
			// console.log(lrcs)
			for (var i in lrcs) { //遍历歌词数组
				lrcs[i] = lrcs[i].replace(/(^\s*)|(\s*$)/g, ""); //去除前后空格
				var t = lrcs[i].substring(lrcs[i].indexOf("[") + 1, lrcs[i].indexOf("]")); //取[]间的内容
				var s = t.split(":"); //分离:前后文字
				if (!isNaN(parseInt(s[0]))) { //是数值
					var arr = lrcs[i].match(/\[(\d+:.+?)\]/g); //提取时间字段，可能有多个
					var start = 0;
					for (var k in arr) {
						start += arr[k].length; //计算歌词位置
					}
					var content = lrcs[i].substring(start); //获取歌词内容
					for (var k in arr) {
						var t = arr[k].substring(1, arr[k].length - 1); //取[]间的内容
						var s = t.split(":"); //分离:前后文字
						state.ms.push({ //对象{t:时间,c:歌词}加入ms数组
							t: (parseFloat(s[0]) * 60 + parseFloat(s[1])).toFixed(3),
							c: content
						});
					}
				}
			}
		},
		touchStart(state, event) {
			// 记录触摸开始时的Y坐标
			state.startY = event.touches[0].clientY;
			state.dragging = true; // 开始拖动
		},
		touchMove(state, event) {
			if (state.dragging) {
				event.preventDefault(); // 防止滚动
				let moveY = event.touches[0].clientY - state.startY;
				// 更新内容的位置
				moveY = moveY < 0 ? 0 : moveY
				state.moveY = moveY;
			}
		},
		touchEnd(state, val) {
			state.dragging = false; // 结束拖动

			if (state.moveY > state.threshold) {
				if (val === 'show') {
					state.show = false
				} else {
					state.isaudio = false
				}
			}
			state.moveY = 0;

		},
		lyricTouch(state) {
			state.srcollShow = true;
		},
		lyricTouchEnd(state) {
			setTimeout(() => {
				state.srcollShow = false
			}, 5000)
		},
		lyricScroll(state, e) {
			if (!state.srcollShow) return;
			let systemInfo = uni.getSystemInfoSync()
			let moveTom = systemInfo.windowHeight * 0.07;
			let i = state.scrollIndex = Math.floor(e.detail.scrollTop / moveTom);
			state.nowPlayTime = (state.ms[i]) ? state.ms[i].t : 0;
		},
		JumpPlayTime(state) {
			if (state.nowPlayTime != 0) {
				// state.audio.play()
				state.audio.seek(state.nowPlayTime);
			}
		},
		updateSrc(state) {
			setTimeout(() => {
				state.islike = !state.islike
			}, 200);
		},
		showCore(state, val) {
			if (val == 'cd') {
				state.cdFade = 0
				state.lrcFade = 1
			} else {
				state.lrcFade = 0
				state.cdFade = 1
			}
			setTimeout(() => {
				state.isCore = !state.isCore
				state.srcollShow = false;
			}, 500);
		},
		watchMoveY(state, newVal) {
			if (state.show && state.isaudio) {
				state.list_moveBy = newVal + 'rpx'
			} else {
				state.lyric_moveby = state.isaudio ? newVal + 'rpx' : 0
				state.list_moveBy = state.show ? newVal + 'rpx' : 0
			}
		},
		watchCurrentTime(state, newVal) {
			let systemInfo = uni.getSystemInfoSync()
			let moveTom = systemInfo.windowHeight * 0.07;
			for (let i = 0; i < state.ms.length; i++) {
				try {
					if (Math.floor(newVal) >= Math.floor(state.ms[i].t)) {
						if ((Math.floor(newVal) < Math.floor(state.ms[i + 1].t)) && i < state.ms.length) {
							state.msIndex = i;
							if (state.srcollShow) return;
							// state.scrollTop = (moveTom * i) + 25;
							state.scrollTop = (moveTom * i);
						}
					}
				} catch (err) {
					state.msIndex = i;
					state.scrollTop = (moveTom * i) + 25;
				}
			}
		},
		watchScrollTop(state, newVal) {
			let systemInfo = uni.getSystemInfoSync()
			let moveTom = systemInfo.windowHeight * 0.07;
			this.msIndex = Math.floor(newVal / moveTom);
		}
	},

	// 模块的 getters 属性
	getters: {

	},
}